package controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Repository;
import model.UserRepository;
import myBeans.LoginInfo;
import myBeans.UserInfo;

/**
 * Servlet implementation class JoinWithMeServlet
 */
@WebServlet("/JoinWithMeServlet")
public class JoinWithMeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public JoinWithMeServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			processRequest(request, response);
		} catch (NamingException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			processRequest(request, response);
		} catch (NamingException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException,
			NamingException, SQLException {

		UserInfo user = new UserInfo();
		user.setDisplayName(request.getParameter("displayname"));

		String confirm_password = request.getParameter("confirm_password");
		if (confirm_password.equals(request.getParameter("password"))) {
			user.setPassword(confirm_password);
			user.setUserName(request.getParameter("name"));
			user.setMobileNo(request.getParameter("mobileno"));
			user.setUserId(request.getParameter("userid"));

			String userrole = request.getParameter("userrole");
			System.out.println("userrole : " + userrole);

			if (userrole.equals("User")) {
				user.setRole("User");
			} else if (userrole.equals("Writer")) {
				user.setRole("Writer");
			} else if (userrole.equals("Publishing")) {
				user.setRole("Market");
			} else {
				user.setRole("default");
			}

			// HttpSession session = request.getSession(true);

			UserRepository userRepo = new UserRepository();
			boolean IsExistUser = userRepo.IsExistsUser(user.getUserId());

			if (IsExistUser) {
				response.sendRedirect("JoinWithMe_UserExists.jsp");
				return;
			}

			boolean IsSuccess = userRepo.CreateMember(user);

			if (IsSuccess) {
				response.sendRedirect("JoinWithMe_Success.jsp");
			} else {
				response.sendRedirect("JoinWithMe_Fail.jsp");
			}
		} else {
			response.sendRedirect("JoinWithMe_Fail.jsp");
		}
	}

	private boolean validateUser(UserInfo user, HttpSession session) {
		if (user.getDisplayName().isEmpty()) {
			session.setAttribute("JoinWithMeErrorMessage", "���ὧ");
			return false;
		}
		return true;
	}
}
